/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.editor.field;

import java.io.Serializable;

import com.daikit.daikit4gxt.client.action.DkPagingRpcCallerWithInput;
import com.daikit.daikit4gxt.client.action.DkPagingToolbar;
import com.daikit.daikit4gxt.client.labelprovider.DkAsSimpleStringLabelProvider;
import com.daikit.daikit4gxt.client.ui.editor.field.DkComboboxCell.MyDynamicComboboxCssBundle;
import com.daikit.daikit4gxt.client.ui.editor.field.DkComboboxCell.MyDynamicComboboxXTemplate;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;


/**
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <B>
 */
public class DkDynamicAsyncPagingCombobox<B extends Serializable> extends DkComboBox<B>
{

	/**
	 * Wrapper to
	 * {@link #DkDynamicAsyncPagingCombobox(ModelKeyProvider, DkPagingRpcCallerWithInput, MyDynamicComboboxXTemplate, MyDynamicComboboxCssBundle)}
	 * with a {@link DkAsSimpleStringLabelProvider}
	 * 
	 * @param modelKeyProvider
	 *           the {@link ModelKeyProvider} for the underlying store
	 * @param rpcCaller
	 *           the {@link DkPagingRpcCallerWithInput}
	 */
	public DkDynamicAsyncPagingCombobox(final ModelKeyProvider<? super B> modelKeyProvider,
			final DkPagingRpcCallerWithInput<B> rpcCaller)
	{
		this(modelKeyProvider, new DkAsSimpleStringLabelProvider<B>(), rpcCaller);
	}

	/**
	 * Create a new {@link DkComboBox} supporting paging
	 * 
	 * @param modelKeyProvider
	 *           the {@link ModelKeyProvider} for the underlying store
	 * @param labelProvider
	 *           the {@link LabelProvider} it will be used for ComboBox content and choices
	 * @param rpcCaller
	 *           the {@link DkPagingRpcCallerWithInput}
	 */
	public DkDynamicAsyncPagingCombobox(final ModelKeyProvider<? super B> modelKeyProvider, final LabelProvider<B> labelProvider,
			final DkPagingRpcCallerWithInput<B> rpcCaller)
	{
		super(new DkDynamicAsyncPagingComboboxCell<B>(new ListStore<B>(modelKeyProvider), labelProvider, rpcCaller));
	}

	/**
	 * Wrapper to
	 * {@link #DkDynamicAsyncPagingCombobox(ModelKeyProvider, LabelProvider, DkPagingRpcCallerWithInput, MyDynamicComboboxXTemplate, MyDynamicComboboxCssBundle)}
	 * with a {@link DkAsSimpleStringLabelProvider}
	 * 
	 * @param modelKeyProvider
	 *           the {@link ModelKeyProvider} for the underlying store
	 * @param rpcCaller
	 *           the {@link DkPagingRpcCallerWithInput}
	 * @param choicesTemplate
	 *           it will be used for choices and return HTML
	 * @param cssBundle
	 *           a clientBundle permitting to retrieve a CSS file to be used to render choicesTemplate
	 * 
	 */
	public DkDynamicAsyncPagingCombobox(final ModelKeyProvider<B> modelKeyProvider, final DkPagingRpcCallerWithInput<B> rpcCaller,
			final MyDynamicComboboxXTemplate<B> choicesTemplate, final MyDynamicComboboxCssBundle<?> cssBundle)
	{
		this(modelKeyProvider, new DkAsSimpleStringLabelProvider<B>(), rpcCaller, choicesTemplate, cssBundle);
	}

	/**
	 * Create a new {@link DkComboBox} supporting paging
	 * 
	 * @param modelKeyProvider
	 *           the {@link ModelKeyProvider} for the underlying store
	 * @param labelProvider
	 *           the {@link LabelProvider} it will be used for ComboBox content
	 * @param rpcCaller
	 *           the {@link DkPagingRpcCallerWithInput}
	 * @param choicesTemplate
	 *           it will be used for choices and return HTML
	 * @param cssBundle
	 *           a clientBundle permitting to retrieve a CSS file to be used to render choicesTemplate
	 */
	public DkDynamicAsyncPagingCombobox(final ModelKeyProvider<B> modelKeyProvider, final LabelProvider<B> labelProvider,
			final DkPagingRpcCallerWithInput<B> rpcCaller, final MyDynamicComboboxXTemplate<B> choicesTemplate,
			final MyDynamicComboboxCssBundle<?> cssBundle)
	{
		super(new DkDynamicAsyncPagingComboboxCell<B>(new ListStore<B>(modelKeyProvider), labelProvider, rpcCaller,
				choicesTemplate, cssBundle));
	}

	/**
	 * @return the pagingToolBar
	 */
	public DkPagingToolbar<B, PagingLoadConfig, PagingLoadResult<B>> getPagingToolBar()
	{
		return ((DkDynamicAsyncPagingComboboxCell<B>) getCell()).getPagingToolbar();
	}

	/**
	 * Display or hide toolbar menu (useful when toolbar is not large enough)
	 * 
	 * @param showMenu
	 *           = true by default
	 */
	public void setShowToolbarMenu(final boolean showMenu)
	{
		getPagingToolBar().setShowMenu(showMenu);
	}

}
